/**
* Class Name: ifulfillProductionReady
* Author: Accenture
* Date: 13-Sep-2011
* Requirement/Project Name: iFulfill
* Requirement/Project Description: Update the production status when set production ready button is clicked.
**/


global  with sharing class ifulfillProductionReady{

   //update the FI to production ready.    
    WebService static void updateSetProductionReady(String id)
    {   
        Fullfilment_Item__c fiObj = null;
        List<Fullfilment_Item__c>  fiList = [select Related_Case_Id__c,Related_Case_Id__r.Status,Status__c,Production_staus__c,Workflow_End_Date__c from Fullfilment_Item__c where id = :id];
        if(fiList.size() > 0)
        {
            fiObj = fiList.get(0); // no need to bulkify since code being called from button on FI detail page
            fiObj.Workflow_End_Date__c = DateTime.now();
            update fiObj;
            try
            {
                update fiObj;
            }
          
            catch(Exception ex )
            {
                GeneralConstants.mailAnyExceptionToMe('ifulfillProductionReady','updateSetProductionReady',ex);
                System.debug('Error while updating FI record' + ex);
            }           
        }        
    }
  
 
    /** To populate Requested date on clicking Submit button on Query Request page **/
    WebService static void updateQueryRequestDate(String id)
    {
        Query_Request__c queryRequestObj = null;
        List<Query_Request__c>  qrList = [select Query_Requested_On__c  from Query_Request__c where id = :id];
        if(qrList.size() > 0)
        {
            queryRequestObj = qrList.get(0);
            queryRequestObj.Query_Requested_On__c = DateTime.now();
             update queryRequestObj;   
        }
          
    
    }
    
     /** Test method.***/
    public  static testmethod void testUpdateProduction() {    
        Fullfilment_Item__c FI = new Fullfilment_Item__c(Main_Listing_Phone_Number__c = '100',Listed_Name__c ='XYZ',Heading_Name__c ='Heading name',Item_Id__c = 'Item id' ,Product_Code__c ='p1',UDAC__c ='Udac1',Region__c = 'region1',Transaction_Type__c ='New');
        insert FI;
        ifulfillProductionReady.updateSetProductionReady(FI.id);
    }
    
     /** Test method.***/
    public  static testmethod void testpdateQueryRequestDate() 
    {    
         Profile p = [select id from profile where name='System Administrator'];         
        User u = new User(alias = 'sysad', email='sysadmin@accenture.com',emailencodingkey='UTF-8', lastname='Tes12', languagelocalekey='en_US',localesidkey='en_US', profileid = p.Id,timezonesidkey='Asia/Calcutta', username='testad123@testorg.com' );           
        Insert u; 
        Fullfilment_Item__c FI4 = new Fullfilment_Item__c(Product_Family__c = 'Banner' ,Main_Listing_Phone_Number__c = '104560',Listed_Name__c ='XIne',Heading_Name__c ='Test Get grouped',Item_Id__c = 'Item id123' ,Product_Code__c ='p1',UDAC__c ='Udac1',Region__c = 'region1',Transaction_Type__c ='New', ownerid = u.id,group_id__c =2);
        insert FI4 ;
        Fullfilment_Item__c F = [select ownerID from  Fullfilment_Item__c  where id = :FI4.id ];
        F.ownerID = u.id;
        update f;
        
        Query_Request__c qr = new  Query_Request__c(Fulfillment_Item__c = FI4.ID,Request_Comments__c='Test Comment',Request_Reason__c= 'Reason 1');
        insert qr;
        
        ifulfillProductionReady.updateQueryRequestDate(qr.id);
    }

}